<?php
/*
 * @Description    : 
 * @Author         : QianLong
 * @Date           : 2023-01-04 11:42:07
 * @LastEditors    : QianLong
 * @Site           : http://www.21ds.cn
 * @LastEditTime   : 2023-04-21 15:53:58
 */

declare(strict_types=1);
/*
 * 请求校验
 */

namespace App\Service;

use Hyperf\Context\Context;

class DataResource
{
    /**
     * 获取用户ID
     */
    public function getUserId()
    {
        return Context::get('uid');
    }

    public function getUserInfo()
    {
        return Context::get('userInfo');
    }

    public function getPlatSetting()
    {
        return Context::get('platSetting');
    }

    public function getUserEmail()
    {
        return Context::get('email');
    }

    public function getClientIp($request)
    {
        $ipArr = $request->getHeader('x-forwarded-for');
        if (empty($ipArr)) {
            $ipArr = $request->getHeader('x-real-ip');
        }
        // $ipArr = $request->getHeader('x-real-ip');
        // if (empty($ipArr)) {
        //     $ipArr = $request->getHeader('x-forwarded-for');
        // }
        unset($request);
        if (isset($ipArr[0]) && !empty($ipArr[0])) {
            $ipArray = explode(',',$ipArr[0]);
            return $ipArray[0];
        } else {
            return '';
        }
    }
    public function getUserAgent($request)
    {
        $ipArr = $request->getHeader('User-Agent');
        if (empty($ipArr)) {
            $ipArr = $request->getHeader('user-agent');
        }
        unset($request);
        if (isset($ipArr[0]) && !empty($ipArr[0])) {
            return $ipArr[0];
        } else {
            return '';
        }
    }
}
